package com.quanyan.vote.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.vote.cache.MyRedisTemplate;
import com.quanyan.vote.constans.VoteConstans;
import com.quanyan.vote.entity.db.*;
import com.quanyan.vote.entity.vo.req.ReqDayIncrementMsg;
import com.quanyan.vote.entity.vo.req.ReqExpertAdmin;
import com.quanyan.vote.entity.vo.req.ReqVoteDetailAdmin;
import com.quanyan.vote.entity.vo.resp.RespExpertAdmin;
import com.quanyan.vote.entity.vo.resp.RespVoteDetailAdmin;
import com.quanyan.vote.mapper.*;
import com.quanyan.vote.service.TbVoteExpertAdminService;
import com.quanyan.vote.utils.VoteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by Administrator on 2016/7/26.
 */
@Service("tbVoteExpertAdminServiceImpl")
public class TbVoteExpertAdminServiceImpl implements TbVoteExpertAdminService{

    private static final Logger LOGGER = LoggerFactory.getLogger(TbVoteExpertAdminServiceImpl.class);

    @Autowired
    private TbVoteAdminMapper tbVoteAdminMapper;

    /**
     * 达人排名
     * @param reqExpertAdmin
     * @return
     */
    private Map<String,Object> getParamMap(ReqExpertAdmin reqExpertAdmin){
        Map<String,Object> paramMap=new HashMap<>();
        String id=reqExpertAdmin.getId();
        if (null!=id && VoteUtil.checkNumber(id)){
            paramMap.put("id",id);
        }else{
            paramMap.put("id","");
        }
        paramMap.put("city",reqExpertAdmin.getCity());
        paramMap.put("name",reqExpertAdmin.getName());
        paramMap.put("mobile",reqExpertAdmin.getMobile());
        paramMap.put("expert",reqExpertAdmin.getExpert());
        paramMap.put("sport",reqExpertAdmin.getSport());
        paramMap.put("gender",reqExpertAdmin.getGender());
        paramMap.put("orderByVoteCount",reqExpertAdmin.getOrderBy());
        paramMap.put("pageNum",reqExpertAdmin.getPageNum()*reqExpertAdmin.getPageSize());
        paramMap.put("pageSize",reqExpertAdmin.getPageSize());
        return paramMap;
    }
    /**
     * 后管达人排名页面
     * @param reqExpertAdmin
     * @return
     */
    @Override
    public APIResponse<PageObj<List<RespExpertAdmin>>> getExpertAdminMsg(ReqExpertAdmin reqExpertAdmin) {
        Map<String,Object> paramMap=getParamMap(reqExpertAdmin);
        List<RespExpertAdmin> respExpertAdminList=tbVoteAdminMapper.queryExpertDetailAdmin(paramMap);
        int totalCount=tbVoteAdminMapper.queryCountExpertDetailAdmin(paramMap);
        LOGGER.debug("总条数-------------------->>>"+totalCount);
        PageObj<List<RespExpertAdmin>> result= PageObj.create(totalCount,reqExpertAdmin.getPageNum(),reqExpertAdmin.getPageSize(),respExpertAdminList);
        return APIResponse.returnSuccess(result);
    }

    /**
     *导出达人排名数据
     * @param reqExpertAdmin
     * @return
     */
    @Override
    public APIResponse<List<RespExpertAdmin>> importExpertAdminMsg(ReqExpertAdmin reqExpertAdmin) {
        Map<String,Object> paramMap=getParamMap(reqExpertAdmin);
        List<RespExpertAdmin> respExpertAdminList=tbVoteAdminMapper.importExpertDetailAdmin(paramMap);
        return APIResponse.returnSuccess(respExpertAdminList);
    }

    /**
     * 达人详情
     * @param reqVoteDetailAdmin
     * @return
     */
    private Map<String,Object> getDetailParamMap(ReqVoteDetailAdmin reqVoteDetailAdmin){
        Map<String,Object> paramMap=new HashMap<>();
        String id=reqVoteDetailAdmin.getId();
        if (null!=id &&VoteUtil.checkNumber(id)){
            paramMap.put("id",id);
        }else{
            paramMap.put("id","");
        }
        paramMap.put("city",reqVoteDetailAdmin.getCity());
        paramMap.put("name",reqVoteDetailAdmin.getName());
        paramMap.put("mobile",reqVoteDetailAdmin.getMobile());
        paramMap.put("expert",reqVoteDetailAdmin.getExpert());
        paramMap.put("sport",reqVoteDetailAdmin.getSport());
        paramMap.put("gender",reqVoteDetailAdmin.getGender());
        paramMap.put("pageNum",reqVoteDetailAdmin.getPageNum()*reqVoteDetailAdmin.getPageSize());
        paramMap.put("pageSize",reqVoteDetailAdmin.getPageSize());
        paramMap.put("startDate",reqVoteDetailAdmin.getStartDate());
        paramMap.put("endDate",reqVoteDetailAdmin.getEndDate());
        return paramMap;
    }
    /**
     * 后管达人投票详情页面
     * @param reqVoteDetailAdmin
     * @return
     */
    @Override
    public APIResponse<PageObj<List<RespVoteDetailAdmin>>> getVoteDetailAdmin(ReqVoteDetailAdmin reqVoteDetailAdmin) {
        Map<String,Object> paramMap=getDetailParamMap(reqVoteDetailAdmin);
        List<RespVoteDetailAdmin> respVoteDetailAdmins=tbVoteAdminMapper.queryVoteDetailAdmin(paramMap);
        int count=tbVoteAdminMapper.queryCountExpertDetail(paramMap);
        LOGGER.debug("总条数-------------------->>>"+count);
        PageObj<List<RespVoteDetailAdmin>> result= PageObj.create(count,reqVoteDetailAdmin.getPageNum(),reqVoteDetailAdmin.getPageSize(),respVoteDetailAdmins);
        return APIResponse.returnSuccess(result);
    }

    /**
     * 导出达人投票详情数据
     * @param reqVoteDetailAdmin
     * @return
     */
    @Override
    public APIResponse<List<RespVoteDetailAdmin>> importVoteDetailAdmin(ReqVoteDetailAdmin reqVoteDetailAdmin) {
        Map<String,Object> paramMap=getDetailParamMap(reqVoteDetailAdmin);
        List<RespVoteDetailAdmin> respVoteDetailAdmins=tbVoteAdminMapper.importVoteDetailAdmin(paramMap);
        return APIResponse.returnSuccess(respVoteDetailAdmins);
    }

    /**
     * 统计达人每天票增量
     * @return
     */
    @Override
    @Transactional
    public APIResponse<Boolean> countDayIncrement() {
        LOGGER.debug("-------定时任务，跟新达人每日票数--------------");
        //查询达人日增量票数
        List<TbVoteDayIncre> tbVoteExperts=tbVoteAdminMapper.queryDayIncrement();
        //批量插入
        int status=0;
        if(tbVoteExperts.size()>0){
            status=tbVoteAdminMapper.insertDayIncrement(tbVoteExperts);
        }
        return APIResponse.returnSuccess(status>0?true:false);
    }

    @Autowired
    private TbVoteExpertMapper tbVoteExpertMapper;
    /**
     * 获取达人票数日增量信息
     * @return
     */
    @Override
    public APIResponse<PageObj<List<TbVoteDayIncre>>> getDayIncrementMsg(ReqDayIncrementMsg reqDayIncrementMsg) {
        //获取总达人数
        TbVoteExpertExample tbVoteExpertExample=new TbVoteExpertExample();
        int totalCount=tbVoteExpertMapper.countByExample(tbVoteExpertExample);
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("pageNum",reqDayIncrementMsg.getPageNum() * reqDayIncrementMsg.getPageSize());
        paramMap.put("pageSize",reqDayIncrementMsg.getPageSize());
        List<TbVoteDayIncre> tbVoteDayIncreList=tbVoteAdminMapper.queryExpertDayIncrement(paramMap);
        LOGGER.info("---------达人日票数记录数------------------>"+tbVoteDayIncreList.size());
        PageObj<List<TbVoteDayIncre>> result= PageObj.create(totalCount,reqDayIncrementMsg.getPageNum(),reqDayIncrementMsg.getPageSize(),tbVoteDayIncreList);
        return APIResponse.returnSuccess(result);
    }
}